/**
 * 
 */
package com.EstateManager.Dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.EstateManager.Entity.Leaseorsaleinfo;
import com.EstateManager.Entity.User;

/**
 * 创建时间 : 下午2:49:07
 * @author Yonggang Liu
 *
 * EstateLeaseOrSaleDao.java
 */
@Repository("leaseOrSaleDao")
public class LeaseOrSaleDao implements IModelBasicOperate<Leaseorsaleinfo, Integer> {

	@Autowired
	private SessionFactory sessionFactory;
	
	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public static final String STATUE_ONPUBLIC = "发布中";
	public static final String STATUE_END = "已完成";
	public static final String STATUE_CANCEL = "已取消";
	
	
	
	
	@Override
	public Leaseorsaleinfo load(Integer id) throws HibernateException {
		return (Leaseorsaleinfo)this.getCurrentSession().load(Leaseorsaleinfo.class, id); 
	}

	@Override
	public Leaseorsaleinfo get(Integer id) throws HibernateException {
		return (Leaseorsaleinfo)this.getCurrentSession().get(Leaseorsaleinfo.class, id);
	}

	@Override
	public void persist(Leaseorsaleinfo entity) throws HibernateException {
		this.getCurrentSession().persist(entity);
	}

	@Override
	public Integer save(Leaseorsaleinfo entity) throws HibernateException {
		return (Integer)this.getCurrentSession().save(entity);
	}

	@Override
	public void saveOrUpdate(Leaseorsaleinfo entity) throws HibernateException {
		this.getCurrentSession().saveOrUpdate(entity);
	}

	@Override
	public void detete(Leaseorsaleinfo entity) throws HibernateException {
		this.getCurrentSession().delete(entity);
	}

	@Override
	public void flush() throws HibernateException {
		this.getCurrentSession().flush();
	}

	@Override
	public void update(Leaseorsaleinfo entity) throws HibernateException {
		this.getCurrentSession().update(entity);
	}

	@Override
	public void merge(Leaseorsaleinfo entity) throws HibernateException {
		this.getCurrentSession().merge(entity);
	}
	
	/**
	 * 获取租售信息页
	 * @param pageSize
	 * @param pageNnm
	 * */
	public List<Leaseorsaleinfo> getListLeaseorsaleinfos(int pageSize, int pageNum) {
		Query query = this.getCurrentSession().createQuery(""
				+ "select leaseorsaleinfo "
				+ "from Leaseorsaleinfo leaseorsaleinfo");
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNum - 1) * pageSize);
		return (List<Leaseorsaleinfo>)query.list(); 
	}
	
	public List<Leaseorsaleinfo> getListByStatu(int pageSize, int pageNum, String statue) {
		Query query = this.getCurrentSession().createQuery(""
				+ "select leaseorsaleinfo "
				+ "from Leaseorsaleinfo leaseorsaleinfo "
				+ "where leaseorsaleinfo.statue = :statue");
		query.setParameter("statue", statue);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNum - 1) * pageSize);
		return (List<Leaseorsaleinfo>)query.list(); 
	}
	
	/**
	 * 根据房产完发布信息完成状态和用户ID返回数据信息页
	 * @param userId
	 * @param statue
	 * @param pageSize
	 * @param pageNum
	 * 
	 * */
	public List<Leaseorsaleinfo> getUserLeasorsaleInfos(int userId, String statue,int pageSize, int pageNum) {
		Query query = this.getCurrentSession().createQuery(""
				+ "select leaseorsaleinfo "
				+ "from Leaseorsaleinfo leaseorsaleinfo "
				+ "where leaseorsaleinfo.statue = :statue and leaseorsaleinfo.estatebasicinfo in ("
				+ "select estatebasicinfo "
				+ "from Estatebasicinfo estatebasicinfo "
				+ "where estatebasicinfo.user = :user)");
		User user = new User();
		user.setId(userId);
		query.setParameter("statue", statue);
		query.setParameter("user", user);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNum-1) * pageSize);
		return (List<Leaseorsaleinfo>)query.list();
	}

}














